// pages/login/login.js
const app = getApp(),
  base64 = require('../../utils/base64.js'),
  AESCrypt = require('../../utils/AESCrypt.js');
var modal = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    password: '',
    modalHide: true,
    layerColor: 'red',
    layerPic: '/images/unhappy.png',
    layerTitle: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    modal = require('../../utils/modal/modal.js');
    this.layerClose = modal.layerClose;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  userNameInput(e) {
    this.data.username = e.detail.value
  },

  passwordInput(e) {
    this.data.password = e.detail.value
  },

  loginSub() {
    wx.showLoading({
      title: '正在登录...',
    })
    let that = this;
    wx.request({
      url: app.globalData.appUrl + '/auth/oauth/token',
      // +'grant_type=password&username='+that.data.username+'&password='+AESCrypt.Encrypt(that.data.password)+'&client_id=zhcw&client_secret=123456',
      // login-direct
      //'https://www.u-zxe.com:9001' '?username=' + that.data.username + '&password=' + that.data.password
      method: 'POST',
      header: {
        'Authorization': 'Basic ' + base64.base64_encode('zhcw:123456'),
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      // application/json application/form-data  
      dataType: 'json',
      // data: {
      //   "grant_type": "password",
      //   "username": that.data.username,
      //   "password": AESCrypt.Encrypt(that.data.password)
      // },
      data:'grant_type=password&username='+that.data.username+'&password='+encodeURIComponent(AESCrypt.Encrypt(that.data.password)),
      // +'&client_id=zhcw&client_secret=123456'
      success(res) {
        console.log(res, '登录结果')
        // console.log(res.cookies[0].indexOf('SESSIONID'), '字段查找0')
        // console.log(res.cookies[1].indexOf('SESSIONID'), '字段查找1')
        // for (let i = 0; i < res.cookies.length; i++) {
        //   if (res.cookies[i].indexOf('SESSIONID') != -1) {
        //     app.globalData.token = res.cookies[i];
        //   }
        // }
        // if (res.data.status == 1000) {
        //   app.globalData.userData = res.data.data
        //   // app.globalData.token = res.data.data
        //   console.log(app.globalData.userData)
        //   // app.globalData.userId = res.data.data.userId
        //   wx.navigateTo({
        //     url: '/pages/welcome/welcome',
        //   })
        // } 
        if(res.data.access_token){
          console.log(res.data.access_token,'access_token')
          app.globalData.userData={}
          app.globalData.userData.userName=that.data.username
          app.globalData.token = res.data.access_token
            wx.navigateTo({
            url: '/pages/welcome/welcome',
          })
        }else {
          wx.hideLoading()
          that.setData({
            modalHide: false,
            layerTitle: res.data.msg || res.data.message
          })
          setTimeout(function () {
            that.layerClose()
          }, 1800)
        }
      },
      fail(res) {
        wx.hideLoading()
        that.setData({
          modalHide: false,
          layerTitle: '网络错误！'
        })
        setTimeout(function () {
          that.layerClose()
        }, 1800)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareData()
  }
})